package com.qf.web.servlet;

import cn.dsna.util.images.ValidateCode;
import com.qf.domain.Address;
import com.qf.domain.User;
import com.qf.sevice.AddressService;
import com.qf.sevice.Impl.AddressServiceImpl;
import com.qf.sevice.Impl.UserServiceImpl;
import com.qf.sevice.UserService;
import com.qf.utils.Base64Utils;
import com.qf.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * projectName:Shopping
 * author:RongChao
 * time:2022/4/18 15:02
 * description:
 */
@WebServlet("/userservlet")
public class UserServlet extends BaseServlet {

    UserService userService =  new UserServiceImpl();
    AddressService addressService = new AddressServiceImpl();
    public String defaultAddress(HttpServletRequest request, HttpServletResponse response) throws Exception {
        User user = (User) request.getSession().getAttribute("user");
        String aid = request.getParameter("id");
        addressService.setDefaultAddress(aid,user.getId());
        return "redirect:/userservlet?method=getAddress";
    }


        public String updateAddress(HttpServletRequest request, HttpServletResponse response) throws Exception {
            User user = (User) request.getSession().getAttribute("user");
            String name = request.getParameter("name");
            String phone = request.getParameter("phone");
            String detail = request.getParameter("detail");
            int level = Integer.parseInt(request.getParameter("level"));
            int id = Integer.parseInt(request.getParameter("id"));
            //如果为空
            if(StringUtils.isEmpty(name ) || StringUtils.isEmpty(phone) || StringUtils.isEmpty(detail)) {
                System.out.println("数据有误，请重新添加");
                return "redirect:/userservlet?method=getAddress";
            }
            Address address = new Address();
            address.setDetail(detail);
            address.setLevel(level);
            address.setName(name);
            address.setPhone(phone);
            address.setUid(user.getId());
            address.setId(id);
            addressService.update(address);
            return "redirect:/userservlet?method=getAddress";
        }

        public String deleteAddress(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String id = request.getParameter("id");
        addressService.deleteById(id);
        return "redirect:/userservlet?method=getAddress";

    }
        public String addAddress(HttpServletRequest request, HttpServletResponse response) throws Exception {
            User user = (User) request.getSession().getAttribute("user");
            String name = request.getParameter("name");
            String phone = request.getParameter("phone");
            String detail = request.getParameter("detail");
            //如果为空
            if(StringUtils.isEmpty(name ) || StringUtils.isEmpty(phone) || StringUtils.isEmpty(detail)) {
                System.out.println("数据有误，请重新添加");
                return "redirect:/userservlet?method=getAddress";
            }
            Address address = new Address();
            address.setDetail(detail);
            address.setLevel(0);
            address.setName(name);
            address.setPhone(phone);
            address.setUid(user.getId());
            addressService.insert(address);
            return "redirect:/userservlet?method=getAddress";
        }
        public String getAddress(HttpServletRequest request, HttpServletResponse response) throws Exception {
        User user = (User) request.getSession().getAttribute("user");
        List<Address> addList=addressService.findAddListByUid(user.getId());
        request.setAttribute("addList",addList);
        return "forward:/self_info.jsp";
    }
        public String logOut(HttpServletRequest request, HttpServletResponse response) throws Exception{
        request.getSession().removeAttribute("user");
        Cookie cookie = new Cookie("usernameAndPassword",Base64Utils.encode(""));
        cookie.setMaxAge(0);
        response.addCookie(cookie);
        return "forward:/login.jsp";
    }

    public String login(HttpServletRequest request, HttpServletResponse response) throws Exception{
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)){
            return "redirect:/login.jsp";
        }
        //调用业务逻辑
        User user = userService.login(username,password);
        if (user != null){
            request.getSession().setAttribute("user",user);
            String auto = request.getParameter("auto");
            String encode = Base64Utils.encode(username + "#" + password);
            Cookie cookie = new Cookie("usernameAndPassword",encode);

            if ("on".equalsIgnoreCase(auto)){
                cookie.setMaxAge(14 * 24 * 60 * 60);
                cookie.setPath("/");
                response.addCookie(cookie);
            }
            return "redirect:/index.jsp";
        }else{
            return "redirect:/login.jsp";
        }
    }
    public void checkCode(HttpServletRequest request, HttpServletResponse response) throws Exception{
        String code = request.getParameter("code");
        if (StringUtils.isEmpty(code)){
            response.getWriter().print("1");
            return;
        }
        String code1 = (String) request.getSession().getAttribute("code");
        if (code.equalsIgnoreCase(code1)){
            response.getWriter().print("0");
        }else{
            response.getWriter().print("1");
        }
    }

    public void code(HttpServletRequest request, HttpServletResponse response) throws Exception{
        ValidateCode validateCode = new ValidateCode(100,40,4,10);
        String code = validateCode.getCode();
        System.out.println(code);
        request.getSession().setAttribute("code",code);
        validateCode.write(response.getOutputStream());
    }


    public String register(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String gender = request.getParameter("gender");
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password) || StringUtils.isEmpty(email)){
            return "redirect:/register.jsp";
        }
        //检测
        boolean b = userService.checkUserName(username);
        if (b){
            return "redirect:/register.jsp";
        }

        boolean isSuccess=userService.register(username,password,email,gender);
        if (isSuccess){
            return "redirect:/login.jsp";
        }else {
            return "redirect:/register.jsp";
        }
    }



    public void checkUserName(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        if (StringUtils.isEmpty(username)){
            response.getWriter().print("0");
            return;
        }
        //调用特务逻辑
        boolean b = userService.checkUserName(username);
        if (b){
            response.getWriter().print("1");
        }else{
            response.getWriter().print("2");
        }
    }
}
